package com.ems.controller;
import com.ems.entity.TMaterial;
import com.ems.entity.vo.MyParam;
import com.ems.service.ITMaterialService;
import com.ems.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZZH
 * @since 2022-06-27
 */
@RestController
@RequestMapping("/t-material")
public class TMaterialController {
    @Autowired
    ITMaterialService materialService;

    /**
     * 新增物料
     * @param Tmaterial
     * @return
     */
    @PostMapping("/add")
    public ResultUtil add(@RequestBody TMaterial Tmaterial){
        if (materialService.addMaterial(Tmaterial) == 1){
            return  ResultUtil.success("添加成功");
        }
        else {
            return ResultUtil.fail("添加失败");
        }
    }

    /**
     * 根据物料id删除物料
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public ResultUtil deleteById(@RequestParam Integer id){
        int num = materialService.deleteMaterial(id);
        if (num > 0){
            return ResultUtil.success("删除成功",num);
        }
        else {
            return ResultUtil.fail("删除失败");
        }
    }

    /**
     * 报废物料
     * @param id
     * @return
     */
    @PostMapping("/scrap")
    @Transactional
    public ResultUtil scrapById(@RequestParam Integer id){
        if (materialService.selectByMatId(id) != null){
            if (materialService.scrapMaterial(id) > 0){
                return ResultUtil.success("报废成功");
            }
            else {
                return ResultUtil.fail("报废失败");
            }
        }
        else {
            return ResultUtil.fail("该物料不存在");
        }
    }

    /**
     * 根据id修改物料信息
     * @param tMaterial
     * @return
     */
    @PostMapping("/update")
    public ResultUtil update(@RequestBody TMaterial tMaterial){
       int num = materialService.updateMaterial(tMaterial);
        if (num > 0){
            return ResultUtil.success("更新成功",num);
        }
        else {
            return ResultUtil.fail("更新失败");
        }
    }

    /**
     * 获取物料列表
     * @return
     */
    @GetMapping("/getlist")
    public ResultUtil getList(){
        List<TMaterial> materials = materialService.getList();
        if (materials != null){
            return ResultUtil.success(materials);
        }
        else {
            return ResultUtil.fail("获取列表失败");
        }
    }

    /**
     * 调整报警水平
     * @param myParam
     * @return
     */
    @PostMapping("/updateParam")
    public ResultUtil updateParam(@RequestBody MyParam myParam){
        if (materialService.updateParam(myParam) > 0){
            return ResultUtil.success("修改成功");
        }
        else {
            return ResultUtil.fail("修改失败");
        }
    }

    /**
     * 根据物料名称和状态进行模糊，分页查询
     * @param num
     * @param count
     * @param name
     * @param state
     * @return
     */
    @PostMapping("/selectPage")
    public ResultUtil selectByPage(@RequestParam Integer num,
    @RequestParam Integer count, @RequestParam String name,
       @RequestParam Integer state){
        List<TMaterial> materials = materialService.selectByPage(num,
                count,name,state);
        if (materials != null){
            return ResultUtil.success(materials);
        }
        else {
            return ResultUtil.fail("查找失败");
        }
    }

    /**
     * 获取剩余物料及状态
     * @return
     */
    @GetMapping("/getDetail")
    public ResultUtil getDetail(){
        List<TMaterial> list= materialService.getDetail();
        if (list != null){
            return ResultUtil.success(list);
        }else {
            return ResultUtil.fail("获取失败");
        }
    }


}
